package com.compty.paint.domain;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.compty.core.framework.context.spring.SpringContextHolder;
import com.compty.core.utils.StringEncoder;
import com.compty.paint.manager.PaintingManager;
import com.compty.paint.util.Constants;
import com.compty.security.domain.User;

/**
 * @Todo:画家基本信息
 * @author: zhuqinghui
 * @CreateTime:2011-5-31 下午05:27:16
 */
@Entity
@Table(name = "t_painter")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Painter implements java.io.Serializable{

	private static final long serialVersionUID = 9089616874731422250L;
	
	private static String IMAGE_SERVER = Constants.IMAGE_SERVER;
	
	private static int new_day = 2;  //两天之内有更新作品，进行标识
	
	private Long id;
	
	/************ 艺术家姓名 ****************/
	private String name;
	private String englishName;
	
	/************ 艺术家字 ****************/
	private String zi;
	
	/************ 艺术家号 ****************/
	private String hao;
	
	/************ 艺术家类别(所属大类) ****************/
	private CatalogType catalogType;
	
	/************ 出生年份  ****************/
	private String age;
	
	/************ 逝世年份  ****************/
	private String endAge; 
	
	/************ 画家年代及派别  ****************/
	private String decade;  //1：近现代国画名家 2:近现代油画名家3:当代国画名家4:当代油画名家5:古代国画名家6:国外名家
	
	/************ 毕业院校  ****************/
	private String graduatedFrom;
	
	private ProvinceInfo provinceInfo;
	
	private CityInfo cityInfo;
	
	/************ 地址  ****************/
	private String address;
	
	/************ 画家照片  ****************/
	private ImageInfo imageInfo;
	
	/************ 头衔  ****************/
	private String honour;
	
	/************ 学位  ****************/
	private String degree;
	
	/************ 艺术成就  ****************/
	private String description;
	
	/************ 上传状态  ****************/
	private UploadState uploadState;
	
	/************ 审核状态  ****************/
	private AuditState auditState;
	
	/************ 上传时间 ****************/
	private Date uploadTime;
	
	/************ 最后修改时间  ****************/
	private Date modifyTime;
	
	/************ 录入人  ****************/
	private User entryPerson;
	
	/************ 修改人  ****************/
	private User modifyPerson;
	
	/************ 作品总件数  ****************/
	@Transient
	private Integer totalCount = 0;
	
	/************ 已售件数  ****************/
	private Integer soldCount = 0;
	
	/************ 画家类型 ****************/
	private String type; //1表示客座画家(画廊艺术家)，2表示名画名家（管理员录入）
	
	@Transient
	private Integer totalCount_update = 0;  //最近更新作品数量
	
	@Transient
	private String htmlEncode = "";  //处理后的文本
	
	private Market_index maxIndex; //历史最高
	
	private Market_index newIndex; //上一季度指数
	
	private Market_index minIndex;//历史最低
	
	private Market_index newestIndex; //最新的
	
	private String originalImagePath;
	private String smallImagePath;
	private String linkUrl;
	
	private Integer sortFlag = 0;//是否排序标志 0:不排序 1：排序
	private Integer orderNo;//排序位置
	
	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name="name",length=100)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name="age",length = 40)
	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	@Column(name="graduated",length=100)
	public String getGraduatedFrom() {
		return graduatedFrom;
	}

	public void setGraduatedFrom(String graduatedFrom) {
		this.graduatedFrom = graduatedFrom;
	}

	@Column(name="honour",length=200)
	public String getHonour() {
		return honour;
	}

	public void setHonour(String honour) {
		this.honour = honour;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="userinfo_id")
	public User getEntryPerson() {
		return entryPerson;
	}

	public void setEntryPerson(User entryPerson) {
		this.entryPerson = entryPerson;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="modify_userinfo_id")
	public User getModifyPerson() {
		return modifyPerson;
	}

	public void setModifyPerson(User modifyPerson) {
		this.modifyPerson = modifyPerson;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="catalogtype_id")
	public CatalogType getCatalogType() {
		return catalogType;
	}

	public void setCatalogType(CatalogType catalogType) {
		this.catalogType = catalogType;
	}

	@Column(name = "uploadtime")
	public Date getUploadTime() {
		return uploadTime;
	}

	public void setUploadTime(Date uploadTime) {
		this.uploadTime = uploadTime;
	}

	@Column(name = "modifytime")
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="auditstate_id")
	public AuditState getAuditState() {
		return auditState;
	}

	public void setAuditState(AuditState auditState) {
		this.auditState = auditState;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="uploadstate_id")
	public UploadState getUploadState() {
		return uploadState;
	}

	public void setUploadState(UploadState uploadState) {
		this.uploadState = uploadState;
	}

	@Column(name = "zi",length = 30)
	public String getZi() {
		return zi;
	}

	public void setZi(String zi) {
		this.zi = zi;
	}

	@Column(name = "hao",length = 30)
	public String getHao() {
		return hao;
	}

	public void setHao(String hao) {
		this.hao = hao;
	}

	@Column(name="degree",length = 10)
	public String getDegree() {
		return degree;
	}

	public void setDegree(String degree) {
		this.degree = degree;
	}
	
	@ManyToOne(fetch = FetchType.LAZY,cascade =  {CascadeType.REMOVE })
	@JoinColumn(name="image_id")
	public ImageInfo getImageInfo() {
		return imageInfo;
	}

	public void setImageInfo(ImageInfo imageInfo) {
		this.imageInfo = imageInfo;
	}

	@Transient
	public Integer getTotalCount() {
		PaintingManager  paintingManager = SpringContextHolder.getBean("paintingManager");
		List<Painting> paintingList = paintingManager.findAvailablePaintingsByPainter(id);
		this.totalCount = paintingList.size();
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	@Column(name = "sold_count")
	public Integer getSoldCount() {
		return soldCount;
	}

	public void setSoldCount(Integer soldCount) {
		this.soldCount = soldCount;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="province_id")
	public ProvinceInfo getProvinceInfo() {
		return provinceInfo;
	}

	public void setProvinceInfo(ProvinceInfo provinceInfo) {
		this.provinceInfo = provinceInfo;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="city_id")
	public CityInfo getCityInfo() {
		return cityInfo;
	}

	public void setCityInfo(CityInfo cityInfo) {
		this.cityInfo = cityInfo;
	}

	@Column(name="endage",length = 40)
	public String getEndAge() {
		return endAge;
	}

	public void setEndAge(String endAge) {
		this.endAge = endAge;
	}

	@Column(name="decade",length = 20)
	public String getDecade() {
		return decade;
	}

	public void setDecade(String decade) {
		this.decade = decade;
	}

	@Column(name="address",length = 200)
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Column(name="type",length = 20)
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	/**
	 *@Todo:查询作品更新数量
	 *@CreateTime:2011-9-22 下午05:54:38
	 * @return
	 */
	@Transient
	public Integer getTotalCount_update() {
		
		PaintingManager  paintingManager = SpringContextHolder.getBean("paintingManager");
		
		Date beginTime = new Date(Calendar.getInstance().getTimeInMillis() - new_day*24*3600*1000);
		
		this.totalCount_update = paintingManager.findAvailablePaintingsCountByPainter(this.id,beginTime);
		
		return totalCount_update;
	}

	public void setTotalCount_update(Integer totalCountUpdate) {
		totalCount_update = totalCountUpdate;
	}

	@Transient
	public String getHtmlEncode() {
		try {
			this.htmlEncode = StringEncoder.dealOriginalText(description);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return htmlEncode;
	}

	public void setHtmlEncode(String htmlEncode) {
		this.htmlEncode = htmlEncode;
	}

	@Column(name="description",length = 2000)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="max_id")
	public Market_index getMaxIndex() {
		return maxIndex;
	}

	public void setMaxIndex(Market_index maxIndex) {
		this.maxIndex = maxIndex;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="min_id")
	public Market_index getMinIndex() {
		return minIndex;
	}

	public void setMinIndex(Market_index minIndex) {
		this.minIndex = minIndex;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="new_id")
	public Market_index getNewIndex() {
		return newIndex;
	}

	public void setNewIndex(Market_index newIndex) {
		this.newIndex = newIndex;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="newest_id")
	public Market_index getNewestIndex() {
		return newestIndex;
	}

	public void setNewestIndex(Market_index newestIndex) {
		this.newestIndex = newestIndex;
	}
	
	@Column(name="englishname",length = 50)
	public String getEnglishName() {
		return englishName;
	}

	public void setEnglishName(String englishName) {
		this.englishName = englishName;
	}

	@Transient
	public String getOriginalImagePath() {
		if( imageInfo!= null ){
			originalImagePath = prefixUrlOfImage()+ imageInfo.getId()+"." + imageInfo.getSuffix();
		}
		return originalImagePath;
	}
	
	@Transient
	public String getSmallImagePath() {
		if( imageInfo!= null){
			smallImagePath = prefixUrlOfImage()+ imageInfo.getId() +"_s."+ imageInfo.getSuffix();
		}
		return smallImagePath;
	}
	
	@Transient
	public String getLinkUrl() {
		if( "2".equals(type)){
			this.linkUrl = "mj/mjxq_"+id+".html";
		}else{
			this.linkUrl = englishName;
		}
		return linkUrl;
	}

	private String prefixUrlOfImage(){
		return IMAGE_SERVER+"painter/";
	}

	@Column(name="sortflag")
	public Integer getSortFlag() {
		return sortFlag;
	}

	public void setSortFlag(Integer sortFlag) {
		this.sortFlag = sortFlag;
	}

	@Column(name="order_no")
	public Integer getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(Integer orderNo) {
		this.orderNo = orderNo;
	}
	
}
